package ciyuan.Gui.Bar;

import ciyuan.DataManager.PlayerDataManager;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.gui.ForgeIngameGui;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import static ciyuan.Constants.ExpLevel.needExp;
import static ciyuan.Constants.PlayerDataType.*;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class Hud extends AbstractGui {

    private static volatile Hud INSTANCE;

    @SubscribeEvent
    public static void onOverlayRender(RenderGameOverlayEvent event) {

        if (event.getType() != RenderGameOverlayEvent.ElementType.ALL) {
            return;
        }
        if (Minecraft.getInstance().player == null) {
            return;
        }
        Hud hud;
        if (INSTANCE == null) {
            synchronized (Hud.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Hud();
                }
            }
        }
        hud = INSTANCE;
        hud.width = Minecraft.getInstance().getWindow().getGuiScaledWidth();
        hud.height = Minecraft.getInstance().getWindow().getGuiScaledHeight();

        hud.matrixStack = event.getMatrixStack();

        ForgeIngameGui.renderFood = false;
        ForgeIngameGui.renderHealth = false;
        ForgeIngameGui.renderArmor = false;


        if (hud.getTemp() == 0.0d) {
            hud.render0t();
        }
        if (hud.getTemp() > 0.0d) {
            if (hud.getTemp() <= 10.0d) {
                hud.render10t();
            }
            if (hud.getTemp() > 10.0d) {
                if (hud.getTemp() <= 20.0d) {
                    hud.render20t();
                }
                if (hud.getTemp() > 20.0d) {
                    if (hud.getTemp() <= 30.0d) {
                        hud.render30t();
                    }
                    if (hud.getTemp() > 30.0d) {
                        if (hud.getTemp() <= 40.0d) {
                            hud.render40t();
                        }
                        if (hud.getTemp() > 40.0d) {
                            if (hud.getTemp() <= 50.0d) {
                                hud.render50t();
                            }
                            if (hud.getTemp() > 50.0d) {
                                if (hud.getTemp() <= 60.0d) {
                                    hud.render60t();
                                }
                                if (hud.getTemp() > 60.0d) {
                                    hud.render70t();
                                }
                            }
                        }
                    }
                }
            }
        }
        hud.render0t();
        hud.renderBar();
        if (renderHealth) {
            hud.renderHealth();
        }
        if (renderFatigue) {
            hud.renderFatigued();
        }
        hud.renderHealthPercent();
        hud.renderHungryPercent();
        hud.renderEmpiricalBar();
        hud.renderEmpirical2();
        hud.renderEmpirical1();

        hud.renderFont();
    }

    public static final double maxWater = 10000.0;
    public long maxExp = 0;
    public long exp = 0;
    public int level = 0;

    public static double temp = 10.0d;
    public static boolean renderHealth = false;
    public static boolean renderFatigue = false;
    private int width;
    private int height;
    private final Minecraft minecraft = Minecraft.getInstance();
    private static final ResourceLocation healthbar = new ResourceLocation("ciyuanrpg", "textures/gui/healthbar.png");
    private static final ResourceLocation hungrybar = new ResourceLocation("ciyuanrpg", "textures/gui/hungrybar.png");
    private static final ResourceLocation fatigue = new ResourceLocation("ciyuanrpg", "textures/gui/fatigue.png");
    private static final ResourceLocation health = new ResourceLocation("ciyuanrpg", "textures/gui/health.png");
    private static final ResourceLocation temp0 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer0.png");
    private static final ResourceLocation temp10 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer10.png");
    private static final ResourceLocation temp20 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer20.png");
    private static final ResourceLocation temp30 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer30.png");
    private static final ResourceLocation temp40 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer40.png");
    private static final ResourceLocation temp50 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer50.png");
    private static final ResourceLocation temp60 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer60.png");
    private static final ResourceLocation temp70 = new ResourceLocation("ciyuanrpg", "textures/gui/thermometer70.png");
    private static final ResourceLocation empirical1 = new ResourceLocation("ciyuanrpg", "textures/gui/empiricalvalue.png");
    private static final ResourceLocation empirical2 = new ResourceLocation("ciyuanrpg", "textures/gui/empiricalvalue2.png");
    private static final ResourceLocation empiricalbar = new ResourceLocation("ciyuanrpg", "textures/gui/empiricalbar.png");

    private MatrixStack matrixStack;

    public void renderEmpirical1() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(empirical1);
        int x = (int) (this.empiricalPercent() * 20);
        blit(matrixStack, width / 2 - 11, height / 2 + 69, 0, 0, 22, 22 - x, 22, 22);
    }

    public void renderEmpirical2() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(empirical2);
        blit(matrixStack, width / 2 - 10, height / 2 + 70, 0, 0, 20, 20, 20, 20);
    }

    public void renderEmpiricalBar() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(empiricalbar);
        blit(matrixStack, width / 2 - 20, height / 2 + 88, 0, 0, 39, 9, 39, 9);
    }

    public void renderBar() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(healthbar);
        blit(matrixStack, width / 2 - 91, height / 2 + 80, 0, 9, 90, 9, 90, 28);
        blit(matrixStack, width / 2 - 91, height / 2 + 71, 0, 9, 90, 9, 90, 28);
        blit(matrixStack, width / 2 + 1, height / 2 + 80, 0, 9, 90, 9, 90, 28);
        blit(matrixStack, width / 2 + 1, height / 2 + 71, 0, 9, 90, 9, 90, 28);
    }

    public void renderFatigued() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(fatigue);
        blit(matrixStack, this.width - 47, this.height - 30, 0, 0, 28, 28, 28, 28);
    }

    public void renderHealth() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(health);
        blit(matrixStack, this.width - 75, this.height - 30, 0, 0, 28, 28, 28, 28);
    }

    public void renderHealthPercent() {
        double healthPercent = minecraft.player.getHealth() / minecraft.player.getMaxHealth();
        temp = PlayerDataManager.getDouble(null, TEMPERATURE);
        exp = PlayerDataManager.getLong(null, EXP);
        level = PlayerDataManager.getInt(null, LEVEL);
        maxExp = needExp[level];
        double magicPercent = PlayerDataManager.getLong(null, MAGIC) / (double) PlayerDataManager.getLong(null, MAX_MAGIC);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(healthbar);
        int h = (int) (88 * healthPercent);
        blit(matrixStack, width / 2 - 90, height / 2 + 81, 1, 1, h, 7, 90, 28);
        int m = (int) (88 * magicPercent);
        blit(matrixStack, width / 2 - 90, height / 2 + 72, 1, 19, m, 7, 90, 28);
    }

    public void renderHungryPercent() {
        double hungryPercent = (minecraft.player.getFoodData().getFoodLevel()) / 20.0;
        double waterPercent = PlayerDataManager.getInt(null, WATER) / maxWater;
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(hungrybar);
        int h = (int) (88 * hungryPercent);
        blit(matrixStack, width / 2 + 2 + (88 - h), height / 2 + 81, 1 + (88 - h), 19, h, 7, 90, 28);
        int w = (int) (88 * waterPercent);
        blit(matrixStack, width / 2 + 2 + (88 - w), height / 2 + 72, 1 + (88 - w), 1, w, 7, 90, 28);
    }

    public void renderFont() {

        StringTextComponent healthNumber = new StringTextComponent((int) minecraft.player.getHealth() + "/" + (int) minecraft.player.getMaxHealth());
        StringTextComponent magicNumber = new StringTextComponent((int) PlayerDataManager.getLong(null, MAGIC) + "/" + (int) PlayerDataManager.getLong(null, MAX_MAGIC));
        StringTextComponent hungryNumber = new StringTextComponent(minecraft.player.getFoodData().getFoodLevel() + "/20");
        StringTextComponent waterNumber = new StringTextComponent(PlayerDataManager.getInt(null, WATER) / 10 + "/1000");
        StringTextComponent tempNumber = new StringTextComponent(String.format("%.1f", temp) + "C");
        StringTextComponent empiricalNumber = new StringTextComponent(exp + "/" + maxExp);
        StringTextComponent levelNumber = new StringTextComponent(String.valueOf(level));
        StringTextComponent mcLevel = new StringTextComponent(String.valueOf(minecraft.player.experienceLevel));

        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        drawCenteredString(matrixStack, this.minecraft.font, healthNumber, this.width / 2 - 45, height / 2 + 80, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, magicNumber, this.width / 2 - 45, height / 2 + 71, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, hungryNumber, this.width / 2 + 45, height / 2 + 80, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, waterNumber, this.width / 2 + 45, height / 2 + 71, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, tempNumber, this.width - 12, height - 47, 2293580);
        drawCenteredString(matrixStack, this.minecraft.font, empiricalNumber, width / 2, height / 2 + 88, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, levelNumber, width / 2, height / 2 + 76, 16777215);
        drawCenteredString(matrixStack, this.minecraft.font, mcLevel, width / 2 - 96, height / 2 + 89, 2293580);
    }

    public void render0t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp0);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render10t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp10);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render20t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp20);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render30t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp30);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render40t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp40);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render50t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp50);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render60t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp60);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    public void render70t() {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(temp70);
        blit(matrixStack, this.width - 45, this.height - 38, 0, 0, 36, 36, 36, 36);
    }

    private double empiricalPercent() {
        double e1 = exp;
        double e2 = maxExp;
        return e1 / e2;
    }

    public double getTemp() {
        return temp;
    }

}
